package com.sskj.gcc.ui.fragment;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.rxbus2.BusData;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.NumberUtil;
import com.sskj.gocoin.common.App;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.TradeBean;
import com.sskj.gcc.bean.rxbus.MarketCoinBean;
import com.sskj.gcc.presenter.TradeFragmentPresenter;
import com.sskj.level.bean.BuySellEnum;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.util.CoinUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_FRAGMENT_TRADE)
public class TradeFragment extends BaseFragment<TradeFragmentPresenter> {
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvDirection)
    TextView tvDirection;
    @BindView(R.id.tvPrice)
    TextView tvPrice;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @Autowired
    String code;
    private SlimAdapter slimAdapter;
    DecimalFormat mFormat = new DecimalFormat("0.0000");


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_trade;
    }

    @Override
    public TradeFragmentPresenter getPresenter() {
        return new TradeFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        initRecy();
        setCountTitle(code);
    }

    @Override
    public void initData() {
        mPresenter.getData(code);
        mPresenter.initSocket(code);
    }

    /**
     * K线点击抽屉切换code
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCoin(BusData<CoinBean> busData) {
        if ("KLevel".equals(busData.getId())) {
            code = busData.getData().getCode();
            setCountTitle(code);
            if (tvNum == null)
                return;
            mPresenter.getData(code);
            mPresenter.sendCode(code);
        }
    }

    private void setCountTitle(String code) {
        String name = code.replace("/", "_");
        String unit = "USDT";
        if (name.contains("_")) {
            unit = name.split("_")[1].toUpperCase();
            name = name.split("_")[0].toUpperCase();
        }
        tvPrice.setText(getString(R.string.app_price_usdt) + "(" + unit + ")");
        tvNum.setText(getString(com.sskj.market.R.string.marketdepthFragment1) + "(" + name + ")");
    }

        private void initRecy() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_trade, new SlimInjector<TradeBean.DataBean>() {
            @Override
            public void onInject(TradeBean.DataBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tvTime, data.getAddtime());
                if ("buy".equals(data.getDc())) {
                    injector.text(R.id.tvDirection, BuySellEnum.BUY.getTitle());
                } else {
                    injector.text(R.id.tvDirection, BuySellEnum.SELL.getTitle());
                }
                injector.textColor(R.id.tvDirection, ContextCompat.getColor(App.INSTANCE, "buy".equals(data.getDc()) ?
                        BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()));
                injector.text(R.id.tvPrice, CoinUtil.keepCoinPrice(code,data.getPrice()+""));
                injector.text(R.id.tvNum, NumberUtil.keepMax4(data.getAmount()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCoin(MarketCoinBean marketCoinBean) {
        code = marketCoinBean.getCode();
        if (tvNum == null)
            return;
        String replace = code.replace("_", "/");
        mPresenter.getData(code);
        mPresenter.sendCode(code);
    }

    public void updateUI(List<TradeBean.DataBean> data) {
        if (slimAdapter == null)
            return;
        slimAdapter.updateData(data);
    }

    @Override
    public void onDestroy() {
        mPresenter.closeWebSocket();
        super.onDestroy();
    }

    public void updateData(TradeBean data) {
        if (data.getCode().equals(code)) {
            Flowable.fromIterable(data.getData())
                    .toList()
                    .subscribe((tradeBeans, throwable) -> {
                        slimAdapter.updateData(tradeBeans);
                    });

        }
    }

     public static TradeFragment newInstance(String code) {
        TradeFragment fragment = new TradeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("code", code);
        fragment.setArguments(bundle);
        return fragment;
    }
}
